﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace SweetSQL.Data.Providers
{
    internal sealed class SqlServerConnectionProvider : ConnectionProviderBase
    {
        public SqlServerConnectionProvider(ConnectionParameters parameters)
            : base(parameters)
        { }

        public override void Connect()
        {
            if (!Connected)
            {
                StringBuilder builder = new StringBuilder("Data Source=");
                builder.Append(Parameters.DataSource);
                if (!string.IsNullOrEmpty(Parameters.Database))
                {
                    builder.Append(";Initial Catalog=");
                    builder.Append(Parameters.Database);
                }
                if (!string.IsNullOrEmpty(Parameters.Username))
                {
                    builder.Append(";User ID=");
                    builder.Append(Parameters.Username);
                }
                if (!string.IsNullOrEmpty(Parameters.Password))
                {
                    builder.Append(";Password=");
                    builder.Append(Parameters.Password);
                }
                Util.AppendCustomProperties(builder, Parameters.CustomProperties);

                Connection = new SqlConnection(builder.ToString());
                Connection.Open();
                Connected = true;
            }
        }

        public override IDbCommand CreateCommand(string sql)
        {
            return new SqlCommand(sql, Connection as SqlConnection, Transaction as SqlTransaction) { CommandTimeout = CommandTimeout };
        }

        public override IDataParameter CreateParameter(string name, Type type, int index)
        {
            return new SqlParameter(name, null);
        }

        //public static SqlDbType GetSqlDbType(Type type)
        //{
        //    switch (Type.GetTypeCode(type))
        //    {
        //        case TypeCode.Boolean:
        //            return SqlDbType.Bit;
        //        case TypeCode.Byte:
        //            return SqlDbType.TinyInt;
        //        case TypeCode.Char:
        //            return SqlDbType.Char;
        //        case TypeCode.DateTime:
        //            return SqlDbType.DateTime;
        //        case TypeCode.Decimal:
        //            return SqlDbType.Money;
        //        case TypeCode.Double:
        //            return SqlDbType.Float;
        //        case TypeCode.Int16:
        //        case TypeCode.SByte:
        //            return SqlDbType.SmallInt;
        //        case TypeCode.Int32:
        //        case TypeCode.UInt16:
        //            return SqlDbType.Int;
        //        case TypeCode.Int64:
        //        case TypeCode.UInt32:
        //        case TypeCode.UInt64: //Some numbers do not work here
        //            return SqlDbType.BigInt;
        //        case TypeCode.Single:
        //            return SqlDbType.Real;
        //        case TypeCode.String:
        //            return SqlDbType.NVarChar;
        //        case TypeCode.Object:
        //            if (type == typeof(Guid))
        //            {
        //                return SqlDbType.UniqueIdentifier;
        //            }
        //            else if (type == typeof(TimeSpan))
        //            {
        //                return SqlDbType.Timestamp;
        //            }
        //            else
        //            {
        //                return SqlDbType.Variant;
        //            }
        //        case TypeCode.DBNull:
        //        case TypeCode.Empty:
        //        default:
        //            return SqlDbType.Variant;
        //    }
        //}
    }
}
